﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Serialization;
using System.Diagnostics;

namespace MayhemWinPhone
{
    public class SavedData
    {
        public string ip;
        public string port;

        const string FILE_NAME = "savedData.xml";

        static SavedData()
        {
            Load();
        }

        #region Singleton
        static SavedData instance = null;

        public SavedData()
        {
            ip = "";
            port = "";
        }
        #endregion

        public static string IP
        {
            get
            {
                return instance.ip;
            }
            set
            {
                instance.ip = value;
                instance.Save();
            }
        }

        public static string Port
        {
            get
            {
                return instance.port;
            }
            set
            {
                instance.port = value;
                instance.Save();
            }
        }

        private static void Load()
        {
            SavedData data = null;
            TextReader reader = null;
            try
            {
                using (IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (isoStorage.FileExists(FILE_NAME))
                    {
                        using (IsolatedStorageFileStream file = isoStorage.OpenFile(FILE_NAME, FileMode.Open))
                        {
                            using (reader = new StreamReader(file))
                            {
//                                string s= reader.ReadToEnd();
//                                Debug.WriteLine("XML: \n" + s);
                                XmlSerializer xs = new XmlSerializer(typeof(SavedData));
                                data = xs.Deserialize(reader) as SavedData;
                            }
                        }
                    }
                    else
                    {
                        data = new SavedData();
                    }
                }
            }
            catch(Exception e)
            {
                Debug.WriteLine(e);
                data = new SavedData();
            }
            instance = data;
        }

        private void Save()
        {
            Log.Write("Saving: " + ip + " " + port);
            using (IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream file = isoStorage.OpenFile(FILE_NAME, FileMode.Create))
                {
                    using (StreamWriter writer = new StreamWriter(file))
                    {
                        XmlSerializer xs = new XmlSerializer(typeof(SavedData));
                        xs.Serialize(writer, this);
                    }
                }
            }
        }
    }
}
